C PROGRAMMING
Year 2020 First Semester Paper Solutions
BY DHARMENDRA TANWAR
Computer Science Engineer
YEAR 2020 FIRST SEMESTER
1
YEAR 2020 FIRST SEMESTER
Q.1
(a). Explain the five basic operations performed by any computer system?
Answer: The computer is an electronics device. It takes something as an input, process it, manage it
and then display the result in form of output.
The basic five operations performed by any computer that are Inputting, processing, outputting,
storing and controlling.
1) Inputting: The Inputting is the first basic operations of computer system. Input devices are
used to provide information to the computer. Means inputting is a process by which a user
enters any type of data into a computer system. Computers are not only used to run programs
but also to store data. There are several types of storage devices you can use to store data - a
hard drive, a DVD, a USB flash drive, etc. And computers have several ways to input data
into these storage devices, such as through a USB port or by using a keyboard. If you're
inputting data from another source (such as a camera), it's likely to go through an interface
card before reaching the CPU. We cannot enter any type of data into the computer without the
use of an input device. If you want to enter any data into the computer, then you will have to
use an input device. And there are many types of input devices, like Keyboard, Mouse, Joy
Stick, Light pen, Track Ball, Scanner, Graphic Tablet, Microphone, Magnetic Ink Card,
Reader (MICR), Optical Character Reader (OCR), Bar Code Reader.
2) Processing: Processing is the second basic operations of a computer system. The processing
means, when the computer system starts executing the instructions given by the user, then this
process is called processing.
The processor is the component of a computer that does most of the "work" in terms of
processing. It's responsible for running programs and carrying out instructions from the user.
The processor reads and writes information to and from the hard disk (a type of storage
device) and sends messages to other components in the computer like the monitor, mouse, or
keyboard.
All this data flowing through your PC happens because electricity is running through each
component at a specific speed or frequency. This is called clock speed. The higher the clock
speed, the more quickly information will be processed on your PC. For example, some PCs
may run at 1 GHz (which means one billion cycles per second), while others might run at 800
MHz (800 million cycles per second).
YEAR 2020 FIRST SEMESTER
2
3) Outputting: Outputting is the third basic operations of computer. In simple language,
outputting means, the result of instruction given by a user is called output. Means the process
of providing the results of processing to the user or external devices, such as a monitor,
printer, or speakers.
Whatever input is given by the user computer, then the output result of that input is
outputting.
Input/output devices are responsible for the computer's interaction with the physical world.
For instance, a keyboard, monitor, printer or mouse are input/output devices. For They are
responsible for responding to your commands and providing feedback.
The computer needs these components to provide output, which is information that comes out
of the system. This includes visual feedback like video or text, as well as audio feedback like
sound or music. Hardware is made up of all the physical components that define how your PC
operates - it is what allows you to interact with your PC in some way. Hardware could include
your keyboard, mouse, microphone, or speakers.
These are all hardware pieces that allow you to use your computer in some way.
Software is made up of everything on your screen - everything from Microsoft Word to
Spotify - and it is what allows you to interact with those programs on your computer.
Software could include Microsoft Office or an online game like Candy Crush Saga.
Combined, Hardware and Software make up the "operating system" (OS) of your computer
because they're what makes it function correctly and work with each other.
The OS ( Operating system ) allows input devices like keyboards and mice to work with
output devices like monitors; this means you can control what happens on screen by typing on
a keyboard or moving a mouse around!
We also need drivers (software) for both hardware and software so they will communicate
properly; without drivers installed, we won't be able to do anything.
Without using an output device, we cannot get the output result of any input data.
We have to use an output device to get the output result.
And there are many types of output devices like, Monitor, Printer, Headphones, Computer
Speakers, Projector, GPS, Sound Card, Video Card, Braille Reader, Speech-Generating
Device ...etc.
4) Storing: Storing is the fourth basic operations of computer system. In simple language,
storing means, the output result that comes after executing the instruction given by a user, and
storing that output result is called storing. Means the process of saving data and instructions
in the computer's memory, including both temporary and permanent storage.
The CPU is responsible for storing data in your computer's memory, which is where the data
is stored when you type it into a word document or open an image.
The CPU processes this data to make it easier to retrieve. Processors are the components that
are responsible for executing applications that are stored in your computer's memory.
These processors can be found on the motherboard and can vary depending on the PCs
YEAR 2020 FIRST SEMESTER
3
specifications.
When we save a file, we are copying it from random access memory (RAM) onto a storage
device like an external hard drive, CD/DVD, or flash drive.
When we save a file to our computer's hard drive, we are always saving two copies of the
original file - one copy in its original location and one backup copy in another location.
This redundancy ensures that if something happens to the first file - say by accident deletion -
then there is another copy of it somewhere else on your hard drive.
There are various types of storage device like, Hard Disk Drive (HDD), Floppy Disks, Tapes,
Compact Discs (CDs), DVD and Blu-ray Discs, USB Flash Drives, Secure Digital Cards (SD
Card)s, Solid-State Drives (SSD), RAM, ROM....etc.
5) Controlling: Controlling is the fifth basic operations of computer. Controlling means the
combinations of all computer operations.
Controlling is a type of process that monitors the instruction given by the user from the time it
is executed to the output result. This process itself is called controlling.
In simple language, monitoring means monitoring, which device is doing its work or not.
In a basic computer system, the monitoring work is done by the ALU (Arithmetic Logic
Unit).
controlling refers to the process of managing and directing the various operations of a
computer system. This involves overseeing the execution of instructions, managing input and
output operations, allocating resources such as memory and processing power, and
responding to various events or errors that may occur during operation.
The control unit, which is part of the central processing unit (CPU), is responsible for
managing and coordinating the various operations of the computer system. It interprets
instructions from the program and directs the flow of data and instructions between the
different components of the computer, such as the CPU, memory, and input/output devices.
Controlling is a critical aspect of computer systems as it ensures that the computer operates
efficiently and effectively, and that the different components of the system work together
seamlessly to achieve the desired results.
(b). What is operating system? Explain main functions of on operating system and write
the name of operating system available in the market.
Answer: An Operating System (OS) is an interface between a computer user and computer
hardware. An operating system is a software which performs all the basic tasks like file management,
memory management, process management, handling input and output, and controlling peripheral
YEAR 2020 FIRST SEMESTER
4
devices such as disk drives and printers.
An operating system is software that enables applications to interact with a computer's hardware. The
software that contains the core components of the operating system is called the kernel.
The primary purposes of an Operating System are to enable applications (software) to interact with a
computer's hardware and to manage a system's hardware and software resources.
Or
An Operating System is the low-level software that supports a computer's basic functions, such as
scheduling tasks and controlling peripherals.
Or
An operating system is a program that acts as an interface between the user and the computer
hardware and controls the execution of all kinds of programs.
Or
An operating system (OS) is system software that manages computer hardware, software resources,
and provides common services for computer programs.
The main functions of an operating System: -
1. Memory Management
2. Processor Management
3. Device Management
4. File Management
5. Network Management
6. Security
7. Control over system performance
8. Job accounting
9. Error detecting aids
10. Coordination between other software and users
1. Memory Management: Memory management refers to management of Primary Memory
or Main Memory. Main memory is a large array of words or bytes where each word or byte
has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a program
to be executed, it must in the main memory.
An Operating System does the following activities for memory management
Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part
are not in use.
In multiprogramming, the OS decides which process will get memory when and how
much.
Allocates the memory when a process requests it to do so.
De-allocates the memory when a process no longer needs it or has been terminated.
2. Processor Management: In multiprogramming environment, the OS decides which
process gets the processor when and for how much time. This function is called process
scheduling. An Operating System does the following activities for processor management
YEAR 2020 FIRST SEMESTER
5
Keeps tracks of processor and status of process. The program responsible for this task
is known as traffic controller.
Allocates the processor (CPU) to a process.
De-allocates processor when a process is no longer required.
3. Device Management: An Operating System manages device communication via their
respective drivers. It does the following activities for device management
Keeps tracks of all devices. Program responsible for this task is known as the I/O
controller.
Decides which process gets the device when and for how much time.
Allocates the device in the efficient way.
De-allocates devices.
4. File Management: A file system is normally organized into directories for easy navigation
and usage. These directories may contain files and other directions.
An Operating System does the following activities for file management −
Keeps track of information, location, uses, status etc. The collective facilities are
often known as file system.
Decides who gets the resources.
Allocates the resources.
De-allocates the resources.
5. Other Important Activities: Following are some of the important activities that an
Operating System performs −
Security: By means of password and similar other techniques, it prevents
unauthorized access to programs and data.
Control over system performance: Recording delays between request for a service
and response from the system.
Job accounting: Keeping track of time and resources used by various jobs and users.
Error detecting aids: Production of dumps, traces, error messages, and other
debugging and error detecting aids.
Coordination between other software and users: Coordination and assignment of
compilers, interpreters, assemblers and other software to the various users of the
computer systems.
There are various types of operating system in computer, which are given below.
1. Batch Operating System
2. Time-Sharing Operating System
3. Distributed Operating System
4. Network Operating System
5. Real-Time Operating System
There are following various operating system available in market: -
MS Windows
MS DOS
Ubuntu OS
Mac OS
YEAR 2020 FIRST SEMESTER
6
Apple IOS
Linux OS
UNIX OS
Chrome OS
Fedora OS
(C) (i). Convert (01011.1011)
2
into decimal.
Answer:
Binary Bits
0
1
1
1
.
1
0
1
1
Index
4
3
1
0
-1
-2
-3
-4
(Base)
index
= (2)
index
2
4
2
3
2
1
2
0
2
-1
2
-2
2
-3
2
-4
󰇛

󰇜
󰇛󰇜

= 0*2
4
+ 1*2
3
+ 0*2
2
+ 1*2
1
+ 1*2
0
+ 1*2
-1
+ 0*2
-2
+ 1*2
-3
+ 1*2
-4
= 0 + 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125 + 0.0625
= 11 + 0.6875
= (11.6875)
10
Note: Online calculator for Binary to Other Number System: Click Here
(ii). Perform binary addition: 101101 + 011011 = ?
Answer:
Note: (Procedure to Solve this type of Question) First convert the given operands into decimal
number system and then perform the same operation on those decimal number system. When the
result will come after performing the operation, then that result will have to be converted into the
asked number system or the result will be converted into the same number system in which number
system the operands were earlier.
Given, 101101 + 011011 = (?)
2
(101101)
2
+ (011011)
2
= (?)
2
We know that: (101101)
2
= (45)
10
(011011)
2
= (27)
10
(Don’t write directly, Follow the procedure of Number system conversion).
(101101)
2
+ (011011)
2
= (45)
10
+ (27)
10
= (45 + 27)
10
= (72)
10
We Know that (72)
10
= (1001000)
2
So, 101101 + 011011 = 1001000
YEAR 2020 FIRST SEMESTER
7
(iii). Perform binary subtraction: 101111 - 010101 =?
Answer:
Note: (Procedure to Solve this type of Question) First convert the given operands into decimal
number system and then perform the same operation on those decimal number system. When the
result will come after performing the operation, then that result will have to be converted into the
asked number system or the result will be converted into the same number system in which number
system the operands were earlier.
Given, 101111 - 010101 = (?)
2
(101111)
2
- (010101)
2
= (?)
2
We know that: (101111)
2
= (47)
10
(010101)
2
= (21)
10
(Don’t write directly, Follow the procedure of Number system conversion).
(101111)
2
- (010101)
2
= (47)
10
- (21)
10
= (47 - 21)
10
= (26)
10
We Know that (26)
10
= (011010)
2
So, 101111 - 010101 = 011010
Q.2
(i). What is RAM? Write down the difference between RAM and ROM.
Answer: RAM stands for Random Access Memory, is a hardware device generally located on the
motherboard of a computer and acts as an internal memory of the CPU. It allows CPU store data,
program, and program results when you switch on the computer. As soon as the machine is switched
off, data is erased. It is the read and write memory of a computer, which means the information can be
written to it as well as read from it.
Access time in RAM is independent of the address, that is, each storage location inside the memory is
as easy to reach as other locations and takes the same amount of time. Data in the RAM can be
accessed randomly but it is very expensive. RAM is volatile, i.e., data stored in it is lost when we
switch off the computer or if there is a power failure. Hence, a backup Uninterruptible Power System
(UPS) is often used with computers. RAM is small, both in terms of its physical size and in the
amount of data it can hold.
RAM (Random Access Memory)
YEAR 2020 FIRST SEMESTER
8
RAM
ROM
RAM stands for Random Access Memory. The
data can be written and read.
ROM stands for Read-only Memory. The data
can only be read.
It is a temporary memory of the computer.
It is the permanent memory of the computer.
RAM is a primary memory of the computer that
stores those data and instructions on which the
CPU is currently working.
ROM is a primary memory of the computer that
stores those computer instructions and programs
that do not need to be altered in future like
BIOS.
RAM is a volatile memory. Which means it
stores data as long as power is on.
ROM is a non-volatile memory that means it
stores data permanently even when power is off.
It is a high-speed memory.
It is much slower than the RAM.
The data stored in RAM is used by the Central
Processing Unit (CPU) to process current
instructions
The data stored in ROM is used to bootstrap the
computer.
RAM has a higher capacity compared to ROM.
ROM has a lower capacity compared to RAM.
It is costlier than ROM.
It is cheaper than RAM.
RAM is used as CPU Cache, Primary Memory.
ROM is used as firmware by microcontrollers.
The stored data is easy to access.
The stored data is not as easy to access as it is in
ROM.
(ii). Draw the block diagram of the computer system and explain briefly.
Answer:
Block Diagram of Basic Computer System
YEAR 2020 FIRST SEMESTER
9
I. Input: This is the process of entering data and programs in to the computer system. You
should know that computer is an electronic machine like any other machine which takes as
inputs raw data and performs some processing giving out processed data. Therefore, the input
unit takes data from us to the computer in an organized manner for processing.
II. Storage: The process of saving data and instructions permanently is known as storage. Data
has to be fed into the system before the actual processing starts. It is because the processing
speed of Central Processing Unit (CPU) is so fast that the data has to be provided to CPU
with the same speed. Therefore, the data is first stored in the storage unit for faster access and
processing. This storage unit or the primary storage of the computer system is designed to do
the above functionality. It provides space for storing data and instructions.
The storage unit performs the following major functions:
» All data and instructions are stored here before and after processing.
» Intermediate results of processing are also stored here.
III. Processing: The task of performing operations like arithmetic and logical operations is called
processing. The Central Processing Unit (CPU) takes data and instructions from the storage
unit and makes all sorts of calculations based on the instructions given and the type of data
provided. It is then sent back to the storage unit.
IV. Output: This is the process of producing results from the data for getting useful information.
Similarly, the output produced by the computer after processing must also be kept somewhere
inside the computer before being given to you in human readable form. Again, the output is
also stored inside the computer for further processing.
V. Control: The manner how instructions are executed and the above operations are performed.
Controlling of all operations like input, processing and output are performed by control unit.
It takes care of step-by-step processing of all operations inside the computer.
VI. Central Processing Unit (CPU): The ALU and the CU of a computer system are jointly
known as the central processing unit. You may call CPU as the brain of any computer system.
It is just like brain that takes all major decisions, makes all sorts of calculations and directs
different parts of the computer functions by activating and controlling the operations.
(iii). What are operators? Explain their categories, precedence and associativity with
example of each.
Answer: C operators are one of the features in C which has symbols that can be used to perform
mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language
has a lot of built-in operators to perform various tasks as per the need of the program.
In C Programming, there are eight types of operators available:
1. Increment and decrement operators
2. Arithmetic Operators
3. Relational operators
4. Bitwise operators
5. Logical operators
6. Ternary or Conditional operators
7. Assignment operators
8. Special operators.
YEAR 2020 FIRST SEMESTER
10
Precedence: The precedence of operations determines the order in which operations are performed
first within expressions. High precedence operations are performed before lower precedence
operations. Since parentheses have the highest precedence, operations within parentheses are always
performed first.
Associativity: Operators Associativity is used when two operators of same precedence appear in an
expression. Associativity can be either Left to Right or Right to Left.
1) Increment and Decrement Operators: These operators are useful in minimizing calculation.
n=n+1 can be truncated to n++. The operators are:
a. Increment (++)
b. Decrement (--)
There is a significant difference in usage of the operators depending on the place of application.
Pre-increment operators: if we write the ++ operator before the variable name, one is
added to the operand, and after that, the result is assigned to the variable.
Post-increment operators: if we write the ++ operator after the variable name, the value is
first assigned to the variable, and increment by 1 occurs.
Associativity of Increment and Decrement (Both Pre and Post) operators is Right to Left. And the
Precedence of pre-increment/pre-decrement is very higher than post-increment/post-decrement.
Figure: Source Code File
YEAR 2020 FIRST SEMESTER
11
OUTPUT:
2) Arithmetic Operators: These operators help perform primary arithmetic operations like
multiplying, dividing, adding, subtracting, finding modulus, etc. Means in C Programming, there
are 5 types of Arithmetic operator available.
Operator Name
Symbol
Description
Addition
+
return the addition of two numbers.
Subtraction
-
subtracts one number from another and return result.
Multiplication
*
returns the product of two numbers.
Division
/
returns the quotient when two numbers are divided.
Modulus
%
returns the remainder when two numbers are divided.
Associativity of arithmetic operator is Left to Right.
The precedence of “Multiplication”, “Division” and “Modulus” operators are equal and the
precedence of “Addition” and “Subtraction” operators is equal.
Precedence of Multiplication / Division / Modulus operators is higher than the subtraction /
Addition.
3) Relational Operators: Relational operators are used for comparing two values of quantities
with each other. It establishes a relation between two values.
Note: In C programming languages, we use two ‘==to check the equality, as one ‘=’ sign is used
as an assignment operator. We use six types of relational operators:
The result of relational operator is True or False. In C Programming, every non zero number is
called True and Zero is called False.
Operator Name
Symbol
Description
Greater than
>
Check whether one value is greater than the other one
Greater than equal to
>=
Check if one value is greater than or equal to another one
Lesser than
<
Check whether one value is less than the other one
Lesser than equal to
<=
Check if one value is lesser than or equal to another one
Equal to
==
Checks whether the two operand values are equal
Not equal to
!=
Checks whether the both values are not equal to each other
Associativity of relational operator is Left to Right.
Inially The value of x = 4, y = 7 and z = 0
Increment:
Pre-increment in x: x = 5 and z = 5
Posncrement in y: y = 8 and z = 7
Decrement:
Pre-decrement in x: x = 4 and z = 4
Postdecrement in y: y = 7 and z = 8
--------------------------------
Process exited aer 0.006974 seconds with return value 0
Press any key to connue . . .
YEAR 2020 FIRST SEMESTER
12
The precedence of “greater than, “greater than equal to”, “lesser than and “lesser than
equal to” operators are equal and the precedence of “equal to” and not equal to” operators is
equal.
Precedence of Greater than / Greater than equal to / lesser than / lesser than equal to operators
is higher than the equal to / not equal to.
4) Bitwise Operators: Bitwise operators perform based on Boolean algebra. These operators boost
the efficiency of a program exponentially by increasing the processing speed of programs.
a) Bitwise AND (&): converts the two operands into binary and performs conjunctive
operation bit by bit.
b) Bitwise OR (|): converts the two operands into binary and performs disjunctive operation
bit by bit.
c) Bitwise XOR (^): converts both operands into binary and performs x-or operation bit by
bit.
d) Bitwise ONE’S COMPLEMENT or Bitwise Not operator (~): returns the
complementary form of the operand.
x
y
x & y
x | y
x ^ y
~ x
0
0
0
0
0
1
0
1
0
1
1
1
1
0
0
1
1
0
1
1
1
1
0
0
Bitwise Shift Operator: The bitwise shift operators move the bit values of a binary
object. The left operand specifies the value to be shifted. The right operand specifies
the number of positions that the bits in the value are to be shifted.
Each operand must have an integral or enumeration type. The compiler performs
integral promotions on the operands, and then the right operand is converted to type int.
The result has the same type as the left operand (after the arithmetic conversions).
The right operand should not have a negative value or a value that is greater than or
equal to the width in bits of the expression being shifted. The result of bitwise shifts on
such values is unpredictable.
If the right operand has the value 0, the result is the value of the left operand (after the
usual arithmetic conversions).
e) Bitwise LEFT SHIFT (<<): Indicates the bits are to be shifted to the left.
Eg: N<<i (N: first operand or right value, i: second operand or left value)
Takes two numbers, left shifts the bits of the first operand, the second operand decides the
number of places to shift.
Or in other words left shifting an integer “xwith an integer “y” denoted as ‘(x<<y)is
equivalent to multiplying x with 2^y (2 raised to power y).
Eg: let’s take N=22; which is 00010110 in Binary Form.
Now, if “N is left-shifted by 2i.e N=N<<2 then N will become N=N*(2^2). Thus,
N=22*(2^2)=88 which can be written as 01011000.
YEAR 2020 FIRST SEMESTER
13
f) Bitwise RIGHT SHIFT (>>): Indicates the bits are to be shifted to the right.
Eg: N>>i (N: first operand or right value, i: second operand or left value)
Takes two numbers, right shifts the bits of the first operand, the second operand decides
the number of places to shift.
In other words right shifting an integer “x” with an integer “y” denoted as ‘(x>>y)is
equivalent to dividing x with 2^y.
Eg: let’s take N=32; which is 100000 in Binary Form.
Now, if “N is right-shifted by 2i.e N=N>>2 then N will become N=N/(2^2). Thus,
N=32/(2^2)=8 which can be written as 1000.
Bitwise operators do not work for float or double data types in C.
Associativity of bitwise operator is Left to Right.
The precedence of “Bitwise Left shift” and “Bitwise right shift” operators are equal.
Precedence
Operator
Symbol
Operator Name
Associativity
High (↑↑↑↑↑)
<< , >>
Bitwise shift operator
Left
To
Right
↑↑↑
&
Bitwise and operator
↑↑
^
Bitwise exclusive or (x-or) operator
Low (↑)
|
Bitwise inclusive or operator
5) Logical Operators: We use six logical operators when we need to make decisions by testing
one or more conditions. Thus, logical operators work on Boolean values. The answers returned
are either true or false. In C Programming, every non zero value is true and zero is treated as the
false.
Logical operators are of 3 types:
a) Logical Not Operator ( ! ): It is a unary operator. It is used to reverse the logical state of
its operand. If a condition is true, then Logical NOT operator will make it false.
b) Logical And Operator ( || ): It is a binary operator. If any of the two operands is non-
zero or at least one operand is non zero, then the condition becomes true means the result
is true.
c) Logical Or Operator ( && ): It is a binary operator. If both the operands are non-zero,
then the condition becomes true means result is true.
Example: int a=10, b=0,c=12;
Operation
!a
!b
a&&b
a&&c
a||b
a||c
!(a&&c)
Result
0
1
0
1
1
1
0
Associativity of logical not operator is Right to Left and others Left to Right.
YEAR 2020 FIRST SEMESTER
14
The precedence of “Logical not operator is higher than the “Logical and” operator. And the
precedence “Logical and operator is higher than the Logical or”.
Means the precedence increment order of logical operator is:
logical Not > Logical And > Logical OR.
6) Ternary or Conditional operators: Conditional operator or ternary operator reduces the work
of an if-else block 21 single statement. It is constructed for conditional expressions.
Syntax: VariableName = (condition) ? TrueValue : FalseValue;
Example:
b = 10;
c = 34;
a= (b>c) ? (b+c) : (b-c);
value of a is: (b-c) = -24
Associativity of Ternary or Conditional operator is Right to Left.
7) Assignment Operator: Assignment operators are used to assigning value to a variable. The left
side operand of the assignment operator is a variable and right-side operand of the assignment
operator is a value. The value on the right side must be of the same data-type of the variable on
the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
“=”: This is the simplest assignment operator. This operator is used to assign the value on
the right to the variable on the left.
For example
a = 10;
b = 20;
ch = 'y';
“+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current
value of the variable on left to the value on the right and then assigns the result to the variable on
the left.
Example:
(a += b) can be written as (a = a + b).
If initially value stored in a is 5. Then (a += 6) = 11.
“-=”: This operator is combination of ‘-’ and ‘=’ operators. This operator first subtracts the
current value of the variable on left from the value on the right and then assigns the result to the
variable on the left.
Example:
(a -= b) can be written as (a = a - b).
If initially value stored in a is 8. Then (a -= 6) = 2.
YEAR 2020 FIRST SEMESTER
15
“*=”: This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the
current value of the variable on left to the value on the right and then assigns the result to the
variable on the left.
Example:
(a *= b) can be written as (a = a * b).
If initially value stored in a is 5. Then (a *= 6) = 30.
“/=”: This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current
value of the variable on left by the value on the right and then assigns the result to the variable on
the left.
Example:
(a /= b) can be written as (a = a / b).
If initially value stored in a is 6. Then (a /= 2) = 3.
Associativity of all assignment operators is Right to Left.
The precedence of all assignment operator is equal.
8) Special Operator: Special operators are a special type of operator. In C programming mainly 4
special types of operators.
Operator Symbol
Operator Name
(type)
Type casting Operator
&
Address of operator
*
Indirection or dereference operator
sizeof( )
Size of operator
These all are unary operator.
Associativity of all special type operators is Right to Left.
The precedence of all special type of operator is equal.
Click Here to see the precedence and associativity table of all operator.
Q.3
(i). What is flow chart? Draw the flow chart for finding the roots of a quadratic
equation.
Answer: A flowchart is a diagram that depicts a process, system or computer algorithm. They are
widely used in multiple fields to document, study, plan, improve and communicate often complex
processes in clear, easy-to-understand diagrams.
OR
YEAR 2020 FIRST SEMESTER
16
A flow chart is a graphical representation of algorithms, workflow or process.
The purpose of a flow chart is to provide people with a common language for understanding a project
or process. Each flowchart represents a solution to a given problem definition.
YEAR 2020 FIRST SEMESTER
17
(ii). What is recursion? Write a recursive function to calculate power of a number.
Answer: Recursion is the technique of making a function call itself. This technique provides a way
to break complicated problems down into simple problems which are easier to solve.
Recursion is the process of repeating items in a self-similar way. In programming languages, if a
program allows you to call a function inside the same function, then it is called a recursive call of the
function.
The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function, otherwise it
will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as calculating the
factorial of a number, generating Fibonacci series, etc.
Q.4
(i). Explain the concept of scope and lifetime of a variable with their storage class in C.
Answer: The storage class in the C language for determining the visibility, lifetime, initial value, and
memory location of any given variable. The storage classes define the visibility (scope) and the
lifetime of any function/ variable within a C program. These classes precede the type that they are
going to modify.
A storage class in C is used to represent additional information about a variable.
Storage class represents the scope and lifespan of a variable.
Auto, extern, register, static are the four different storage classes in a C program.
A storage class specifier in C language is used to define variables, functions, and parameters.
auto is used for a local variable defined within a block or function.
register is used to store the variable in CPU registers rather memory location for quick access.
Static is used for both global and local variables. Each one has its use case within a C program.
The lifetime of a variable defines the duration for which the computer allocates memory for it (the
duration between allocation and deallocation of memory).
Global scope: When variable is defined outside all functions. It is then available to all the functions
of the program and all the blocks program contains.
YEAR 2020 FIRST SEMESTER
18
Local scope: When variable is defined inside a function or a block, then it is locally accessible within
the block and hence it is a local variable.
Function scope: When variable is passed as formal arguments, it is said to have function scope.
Storage
Class
Storage
Area
Default Value
Lifetime
Scope
Keyword
Automatic
Memory
Garbage Value
Till control remains
in block
Local
Auto
Register
CPU
Register
Garbage Value
Till control remains
in block
Local
register
Static
Memory
Zero
Value in between
fun. calls
Local
static
External
Memory
Garbage Value
Throughout
program execution
Global
extern
(ii). Write a C Program to find min and max element in an array.
Answer:
YEAR 2020 FIRST SEMESTER
19
OUTPUT:
(iii). Write a C Program to count the number of alphabets in a string.
Answer:
Enter size of the array: 5
Enter elements in array: 24
11
54
32
67
minimum of array is: 11
maximum of array is: 67
--------------------------------
Process exited after 7.655 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
20
OUTPUT:
Q.5
(i). What do you mean by Dynamic Memory Allocation (DMA)? Define Calloc( ),
malloc( ), realloc( ) and Free( ) function.
Answer: Allocation of memory at the time of execution (run time) is known as dynamic memory
allocation. In a simple word, the dynamic memory allocation is the process of assigning the memory
space during the execution time or the run time.
The functions calloc( ) and malloc( ) support allocating of dynamic memory. Dynamic allocation of
memory space is done by using these functions when value is returned by functions and assigned to
pointer variables.
All DMA functions are define in stdlib.h header file.
a) calloc( ): A calloc() function is a predefined library function that stands for contiguous
memory allocation. A calloc() function is used to create multiple blocks at the run time of a
program having the same size in the memory. A calloc function is defined inside the stdlib.h
header file. It has two parameters, no. of blocks and the size of each block. When the dynamic
memory is allocated using the calloc() function, it returns the base address of the first block,
and each block is initialized with 0. And if memory is not created, it returns a NULL pointer.
Syntax: ptr = (cast_type *) calloc ( number_of_blocks, size_of_block);
In the above syntax, the calloc() function has two parameters. The first parameter defines the
number of blocks and the second parameter defines the size of each block in memory. The
size of the blocks and cast_type can be in int, char, float, etc.
Return: It returns the base address of the first block to the ptr variable.
b) malloc( ): The malloc is a predefined library function that stands for memory allocation. A
malloc is used to allocate a specified size of memory block at the run time of a program. It
means it creates a dynamic memory allocation at the run time when the user/programmer does
not know the amount of memory space is needed in the program.
Therefore, it inputs the memory size (in bytes) at the run time to reserve a contiguous memory
block that returns a pointer of type void, which is cast into a pointer of any form. The
dynamic memory is created using the malloc does not initialize the memory at execution time,
and hence the memory block contains some default garbage value. The malloc function is
defined inside the stdlib.h header file. So, we need to use <stdlib.h> header file while using
the malloc function in our program.
Syntax: ptr = ( cast_ type *) malloc (byte_size);
In the above syntax, the byte_size is an argument that specifies the size of the memory block
(in byte), which is passed into the malloc function to reserve the contiguous memory. A
Enter a string: Hello, This is PYQ Solution.
Total number of alphabets in string is: 22
--------------------------------
Process exited after 7.655 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
21
malloc() function returns a type void pointer that can be cast into pointers of any defined
form.
Return: It returns the base address of the first block to the ptr variable.
c) realloc( ): The realloc() function changes the size of a previously reserved storage block.
The ptr argument points to the beginning of the block. The size argument gives the new size
of the block, in bytes. The contents of the block are unchanged up to the shorter of the new
and old sizes.
Syntax: ptr = (cast_type *) realloc(ptr, new_size);
Return: The realloc() function returns a pointer to the reallocated storage block. The storage
location of the block may be moved by the realloc() function. Thus, the ptr argument to the
realloc() function is not necessarily the same as the return value. If size is 0, the realloc()
function returns NULL. If there is not enough storage to expand the block to the given size,
the original block is unchanged and the realloc() function returns NULL.
d) Free( ): free() function should only be used either for the pointers pointing to the memory
allocated using malloc() or for a NULL pointer. free() function only frees the memory from
the heap and it does not call the destructor. To destroy the allocated memory and call the
destructor we can use the delete() operator in C.
Syntax: free(ptr);
The free function does not return any value.
(ii). What is string? Explain five predefined functions of string.
Answer: A string is a sequence of characters terminated with a null character \0. Strings are defined
as an array of characters.
For example: char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it
appends a null character \0 at the end by default.
c
s
t
r
i
n
g
\0
Memory Diagram
There are predefined string functions in the C language, namely string handling functions. There is a
header file defined for these functions named as string.h. While string handling it is important to use a
header file of string.h while doing string handling C.
a) strlen( ): It takes one argument which is string and return the length of string in integer
format. It doesn't count null character '\0'.
Syntax: int strlen(char *str);
Example:
YEAR 2020 FIRST SEMESTER
22
OUTPUT:
b) strcpy( ): If we have to copy the content of one string to another string, then this function is
being used. Even the null characters are copied in the process. In simple word, this function
can copy the content of one string to another.
Syntax: void strcpy(char *destination, char *source);
Example:
Length of string is: 59
--------------------------------
Process exited after 0.01805 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
23
OUTPUT:
c) strcmp( ): To compare two strings to know whether they are same or not we can use
strcmp() function. This string functions in c, compares two strings. While comparing the
strings takes two parameters into account namely: str1,str2.
Syntax: int strcmp(char *str1, char *str2);
On comparing the return value be determined basis the strings setup as shown below.
The function returns a definite value that may be either 0, >0, or <0.
In this function, the two values passed are treated as case sensitive means ‘Aand ‘aare
treated as different letters. The values returned by the function are used as:
a) 0 is returned when two strings are the same
b) If str1<str2 then a negative value is returned
c) If str1>str2 then a positive value is returned
Example:
Value of second string is: Hello This is PYQ solution.
--------------------------------
Process exited after 0.01226 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
24
OUTPUT:
d) strrev( ): If we want to reverse any string without writing any huge or extensive program
manually, then you can use this function. The rev in the strrev() stands for reverse and it is
used to reverse the given string. Function strrev() is used to reverse the content of the string.
Strrev function is used to check the nature of the string, whether the given string is a
palindrome or not. Several other uses and applications are also present in the string reverse
function. This function doesn't return anything but the reversed string is stored in the same
string.
Syntax: void strrev(char *str);
Example:
First String is: C Programmimg.
Second String is: C programming.
First string is lesser than second string.
--------------------------------
Process exited after 0.01864 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
25
OUTPUT:
e) strlwr( ) or strupr( ): Sometimes we may need to convert the lowercase letters of any
string to the uppercase or vice-versa. As it can be understood the lwr stands for lowercase
and upr stands for uppercase. For this purpose there are two direct string functions in C, they
can be used to perform the conversions either from upper to lower case or vice-versa.
The strlwr(string) function returns string characters in lowercase.
The strupr(string) function returns string characters in uppercase.
Syntax:
char* strlwr(char *str);
char* strupr(char *str);
Example:
Enter string: This is PYQs Solution.
String is: This is PYQs Solution.
Reverse String is: .noituloS sQYP si sihT
--------------------------------
Process exited after 28.3 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
26
OUTPUT:
Q.6
(i). What do you mean by structure in C. Explain with an example how we declare and
initialize the structure. What is the difference between structure and Union.
Answer: Structure in c is a user-defined data type that enables us to store the collection of different
data types. Each element of a structure is called a member. Structures ca; simulate the use of classes
and templates as it can store various information.
The 'struct' keyword is used to define the structure.
Example:
We need to manage the record of books in a library. Now a book can have properties like book_name,
author_name, pages and price. So, for any book you need four variables to store its records. Now,
there are two ways to achieve this goal.
The first and the naive one is to create separate variables for each book. But creating so many
variables and assigning values to each of them is impractical. So what would be the optimized and
ideal approach? Here, comes the structure in the picture.
We can define a structure where we can declare the data members of different data types according to
our needs. In this case, a structure named BOOK can be created having three members book_name,
author_name, pages and price. Multiple variables of the type BOOK can be created such as book1,
book2, and so on (each will have its own copy of the three members book_name, author_name, pages
and price).
Declare a structure:
struct BOOK
{
char book_name[30];
char author_name[30];
int pages;
float price;
}book1,book2;
Initialize a structure: Structure members cannot be initialized like other variables inside the
structure definition. This is because when a structure is defined, no memory is allocated to the
structure’s data members at this point. Memory is only allocated when a structure variable is declared.
Consider the following code snippet.
Enter string: Hello, This is PYQs Solutions.
Entered string is: Hello, This is PYQs Solutions.
Converted Lower String is: hello, this is pyqs solutions.
Converted Upper String is: HELLO, THIS IS PYQS SOLUTIONS.
--------------------------------
Process exited after 12.38 seconds with return value 0
Press any key to continue . . .
YEAR 2020 FIRST SEMESTER
27
struct BOOK
{
// structure definition
char book_name[30] = "Let us C"; // COMPILER ERROR
char auther_name[30] = "Yashwant Kanetkar"; // COMPILER ERROR
int pages = 465; // COMPILER ERROR: cannot initialize members here.
float price = 370.0; // COMPILER ERROR: cannot initialize members here.
}b1,b2;
A compilation error will be thrown when the data members of the structure are initialized inside the
structure. To initialize a structure’s data member, create a structure variable. This variable can access
all the members of the structure and modify their values. Consider the following example which
initializes a structure’s members using the structure variables.
struct BOOK
{
char book_name[30];
char author_name[30];
int pages;
float price;
}book1,book2;
int main( )
{
book1.book_name[30]="Let us C";
book1.author_name[30]="Yashwant Kanetkar";
int pages = 465;
float price = 370.0;
return 0;
}
The members of a structure are accessed outside the structure by the structure variables using the dot
operator (.).
Difference between structure and Union:
YEAR 2020 FIRST SEMESTER
28
(ii).
int f(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return f(n-1)+f(n-2);
}
What is the value of f(5)? Explain.
Answer: S
For f(5), function returns the value [f(4) + f(3)].
then, then function calls two time like, f(4) and f(3).
For f(4), function returns the value [f(3) + f(2)].
And f(3), function returns the value [f(2) + f(1)].
And so on, lastly f(0) returns 0 and f(1) returns 1.
Then move backward direction, and perform operation on the return value.
[ f(5) ]
[ f(5): f(4) + f(3) ]
[ f(4): f(3) + f(2) ] + [ f(3): f(2) + f(1) ]
{ [ f(3): f(2) + f(1) ] + [ f(2): f(1) + f(0) ] } + { [ f(2): f(1) + f(0) ] + 1 }
{ { [ f(2): f(1) + f(0) ] + 1 } + [ 1 + 0 ] } + { [ 1 + 0 ] + 1 }
{ { [ 1 + 0 ] + 1 } + [ 1 + 0 ] } + { [ 1 + 0 ] + 1 }
{ { 1 + 1 } + [ 1 ] } + { [ 1 ] + 1 ]
{ 2 + 1 } + { 1 + 1 }
{ 3 } + { 2 }
5
Q.7
(i). What is Pointer? Explain the difference between parameter passing mechanism “call
by value” and “call by reference”. Which is more efficient and why? Explain with
example.
YEAR 2020 FIRST SEMESTER
29
Answer: The pointer in C language is a variable which stores the address of another variable. This
variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends
on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
Example:
int x = 10,*p;
char ch='A';
char *ptr;
p = &x;
ptr = &ch;
Difference between parameter passing mechanism “call by valueand “call by reference”:
Pass-by-reference means to pass the reference of an argument in the calling function to the
corresponding formal parameter of the called function. The called function can modify the value of
the argument by using its reference passed in.
Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments.
The formal parameter is an alias for the argument. When the called function read or write the
formal parameter, it is actually read or write the argument itself.
The difference between pass-by-reference and pass-by-value is that modifications made to arguments
passed in by reference in the called function have effect in the calling function, whereas modifications
made to arguments passed in by value in the called function cannot affect the calling function. Use
pass-by-reference if you want to modify the argument value in the calling function. Otherwise, use
pass-by-value to pass arguments.
The difference between pass-by-reference and pass-by-pointer is that pointers can be NULL or
reassigned whereas references cannot. Use pass-by-pointer if NULL is a valid parameter value or if
you want to reassign the pointer. Otherwise, use constant or non-constant references to pass
arguments.
Example:
YEAR 2020 FIRST SEMESTER
30
#include<stdio.h>
void increment(int);
void incre(int *)
void main()
{
int a = 10;
printf("Call By Value before function calling: a = %d", a);
increment(a);
printf("\nCall by value after function calling: a = %d”, a);
printf("\n\nCall By reference before function calling: a = %d", a);
incre(&a);
printf("\nCall by reference after function calling: a = %d”, a);
getch();
}
void increment(int x)
{
x = x + 1;
printf("value is: %d", x);
}
void incre(int* ptr)
{
*ptr = *ptr + 1;
printf("value is: %d", *ptr);
}
OUTPUT
Call By Value before function calling: a = 10
Value is: 11
Call by value after function calling: a = 10
Call By reference before function calling: a = 10
Value is: 11
Call by reference after function calling: a = 11
YEAR 2020 FIRST SEMESTER
31
(ii). Explain in Detail: (Any two).
A. Compiler, Assembler and Interpreter: reference geeksforgeeks
Compiler: The language processor that reads the complete source program written in high-
level language as a whole in one go and translates it into an equivalent program in machine
language is called a Compiler. Example: C, C++, C#, Java.
In a compiler, the source code is translated to object code successfully if it is free of errors.
The compiler specifies the errors at the end of the compilation with line numbers when there
are any errors in the source code. The errors must be removed before the compiler can
successfully recompile the source code again.
Assembler: The Assembler is used to translate the program written in Assembly language
into machine code. The source program is an input of an assembler that contains assembly
language instructions. The output generated by the assembler is the object code or machine
code understandable by the computer. Assembler is basically the 1st interface that is able to
communicate humans with the machine. We need an Assembler to fill the gap between human
and machine so that they can communicate with each other. code written in assembly
language is some sort of mnemonics(instructions) like ADD, MUL, MUX, SUB, DIV, MOV
and so on. and the assembler is basically able to convert these mnemonics in Binary code.
Here, these mnemonics also depend upon the architecture of the machine.
For example, the architecture of intel 8085 and intel 8086 are different.
Interpreter: The translation of a single statement of the source program into machine code
is done by a language processor and executes immediately before moving on to the next line
is called an interpreter. If there is an error in the statement, the interpreter terminates its
translating process at that statement and displays an error message. The interpreter moves on
to the next line for execution only after the removal of the error. An Interpreter directly
executes instructions written in a programming or scripting language without previously
converting them to an object code or machine code.
Example: Perl, Python and Matlab.
B. Cryptography: Cryptography is technique of securing information and communications
through use of codes so that only those person for whom the information is intended can
understand it and process it. Thus preventing unauthorized access to information. The prefix
“crypt” means “hiddenand suffix graphy means “writing”. In Cryptography the techniques
which are use to protect information are obtained from mathematical concepts and a set of
rule based calculations known as algorithms to convert messages in ways that make it hard to
decode it. These algorithms are used for cryptographic key generation, digital signing,
verification to protect data privacy, web browsing on internet and to protect confidential
transactions such as credit card and debit card transactions.
Or
Cryptography is a method of protecting information and communications through the use of
codes, so that only those for whom the information is intended can read and process it.
In computer science, cryptography refers to secure information and communication
techniques derived from mathematical concepts and a set of rule-based calculations called
algorithms, to transform messages in ways that are hard to decipher. These deterministic
algorithms are used for cryptographic key generation, digital signing, verification to protect
data privacy, web browsing on the internet and confidential communications such as credit
card transactions and email.
YEAR 2020 FIRST SEMESTER
32
Cryptography is closely related to the disciplines of cryptology and cryptanalysis. It includes
techniques such as microdots, merging words with images and other ways to hide information
in storage or transit. However, in today's computer-centric world, cryptography is most often
associated with scrambling plaintext (ordinary text, sometimes referred to as cleartext) into
ciphertext (a process called encryption), then back again (known as decryption). Individuals
who practice this field are known as cryptographers.
C. Cyber Laws: Cyber Law also called IT Law is the law regarding Information-technology
including computers and the internet. It is related to legal informatics and supervises the
digital circulation of information, software, information security, and e-commerce.
IT law does not consist of a separate area of law rather it encloses aspects of contract,
intellectual property, privacy, and data protection laws. Intellectual property is a key element
of IT law. The area of software license is controversial and still evolving in Europe and
elsewhere.
According to the Ministry of Electronics and Information Technology, Government of India :
Cyber Laws yields legal recognition to electronic documents and a structure to support e-
filing and e-commerce transactions and also provides a legal structure to reduce, check cyber
crimes.
Importance of Cyber Law:
It covers all transactions over the internet.
It keeps eye on all activities over the internet.
It touches every action and every reaction in cyberspace.
Cyber laws contain different types of purposes like fraud, copyright, defamation, Harassment
and Stalking, freedom of Speech, .... etc. Some laws create rules for how individuals and
companies may use computers and the internet while some laws protect people from
becoming the victims of crime through unscrupulous activities on the internet.
Refernce : geeksforgeeks
Thank You
Dharmendra Tanwar
Click Me